-- 订单更新量较小，不采取 （1天ODS+29天DWD）方式递归计算；直接取30天ODS数据去重
with delivery_record_new as (
    select
        id
        ,waybill_id
        ,delivery_staff_name
        ,delivery_staff_code
        ,delivery_option
        ,upload_time
        ,push_source
        ,input_time
        ,update_time
        ,is_delete
        ,row_number() over(partition by waybill_id,input_time order by dt desc) as row_id
        ,date_format(input_time, 'yyyy-MM-dd') as dt
    from jms_ods.yl_oms_order_delivery_record
    where dt >= date_add('{{ execution_date | cst_ds }}', -30) and dt <= '{{ execution_date | cst_ds }}'
)

insert overwrite table jms_dwd.dwd_yl_oms_order_delivery_record_base_dt partition(dt)
select
    id
    ,waybill_id
    ,delivery_staff_name
    ,delivery_staff_code
    ,delivery_option
    ,upload_time
    ,push_source
    ,input_time
    ,update_time
    ,is_delete
    ,dt
from  delivery_record_new
where row_id = 1
distribute by 2;